﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FirstGame
{
    public class ButtonItem : MenuItem
    {

        public ButtonItem(ContentManager content,MenuScreen parent, string strTexture1, string strTexture2, Vector2 topleft, Vector2 inittopleft, Vector2 size)
        {
            TopLeft = topleft;
            _InitTopleft = inittopleft;
            _CurrentTopleft = _InitTopleft;
            Size = size;
            topleft = InitMenuItem(content, parent, strTexture1, strTexture2, topleft, inittopleft);
        }

        /// <summary>
        /// create a meuitem from xmlnode
        /// </summary>
        /// <param name="content"></param>
        /// <param name="node"></param>
        public ButtonItem(ContentManager content, MenuScreen parent, XmlNode node)
        {
            float x = float.Parse(node.Attributes["x"].Value);
            float y = float.Parse(node.Attributes["y"].Value);
            float with = float.Parse(node.Attributes["with"].Value);
            float height = float.Parse(node.Attributes["height"].Value);
            TopLeft = new Vector2(x, y);
            Size = new Vector2(with, height);
            string texture1 = node.Attributes["texture1"].Value;
            string texture2 = node.Attributes["texture2"].Value;
            // tao 2 sprite cua MyButton
            //InitMenuItem(content, parent, texture1, texture2, TopLeft, inittopleft);
        }


        private Vector2 InitMenuItem(ContentManager content, MenuScreen parent, string strTexture1, string strTexture2, Vector2 topleft, Vector2 inittopleft)
        {

            _Menuscreen = parent;
            Texture2D[] textures1 = new Texture2D[1];
            textures1[0] = content.Load<Texture2D>(strTexture1);

            Texture2D[] textures2 = new Texture2D[1];
            textures2[0] = content.Load<Texture2D>(strTexture2);

            Sprites = new List<My2Dsprite>();
            My2Dsprite Sprite1 = new My2Dsprite(textures1, _InitTopleft);
            My2Dsprite Sprite2 = new My2Dsprite(textures2, _InitTopleft);

            Sprites.Add(Sprite1);
            Sprites.Add(Sprite2);

            NSprite = Sprites.Count;
            return topleft;
        }
       
        
        public override void Update(GameTime gameTime, InputSystem input)
        {
            if (_Menuscreen.ScreenState == ScreenState.Active)
            {
                t += 0.1f;
                Vector2 temp = this.CurrentTopleft;
                if (this.CurrentTopleft.X < this.TopLeft.X)
                {
                    temp.X = temp.X + t * Velocity.X;
                }
                if (this.CurrentTopleft.X > TopLeft.X)
                {
                    temp.X = TopLeft.X;
                }
                this.CurrentTopleft = temp;
                Sprites[0].Position = this.CurrentTopleft;
                Sprites[1].Position = this.CurrentTopleft;

                if (this.IsClicked(input.CurPoint))
                {
                    IsFocus = true;
                    // khi button duoc focus chua hang tro chuot nam tai vi tri cua button do
                    this.Focus();
                    if (input.IsNewMouseClick(ButtonState.Pressed))
                    {
                        this.Select();
                    }
                }
                else
                {
                    IsFocus = false;
                }

                base.Update(gameTime, input);
            }
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if(IsFocus)
                Sprites[1].Draw(gameTime, spriteBatch, 180);
            else
                Sprites[0].Draw(gameTime, spriteBatch, 180);
        }

        public Vector2 SetRelativePosition()
        {
            Vector2 Temp = new Vector2();
            Temp.X = this.InitTopleft.X - 150;
            Temp.Y = this.InitTopleft.Y + this.Size.Y + 10;
            return Temp;


        }
    }

    public abstract class MenuItem : VisibleGameEntity
    {
        protected Vector2 _InitTopleft;

        public Vector2 InitTopleft
        {
            get { return _InitTopleft; }
            set { _InitTopleft = value; }
        }
        protected Vector2 _CurrentTopleft;

        public Vector2 CurrentTopleft

        {
            get { return _CurrentTopleft; }
            set { _CurrentTopleft = value; }
        }
        protected bool _IsFocus = false;

        public bool IsFocus
        {
            get { return _IsFocus; }
            set { _IsFocus = value; }
        }
        protected Vector2 _Velocity = new Vector2(2f, 0);

        public Vector2 Velocity
        {
            get { return _Velocity; }
            set { _Velocity = value; }
        }


        protected MenuScreen _Menuscreen;

        public MenuScreen Menuscreen
        {
            get { return _Menuscreen; }
            set { _Menuscreen = value; }
        }

        

        protected float t = 0;
        /// <summary>
        /// event focused for item when input focus on this item
        /// 
        /// </summary>
        public event EventHandler Focused;

        public virtual void Focus()
        {
            if (Focused != null)
                Focused(this, EventArgs.Empty);
        }

        /// <summary>
        /// 
        /// </summary>
        public event EventHandler Selected;
        public virtual void Select()
        {
            if (Selected != null)
                Selected(this, EventArgs.Empty);
        }
    }

    

    
}
